<?php
namespace App\Controller;

class MessagesController extends AppController{

    public $displayNum = 30;
    public function initialize()
    {
        parent::initialize();
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.07.03
   * ps：清空指定时间段的已读消息
   * */
    public function cleanMessages()
    {
        $begin_time = $this->request->getData("begin_time");
        $end_time = $this->request->getData("end_time");
        if(empty($begin_time)){
            $this->G->error("begin_time_error");
            return null;
        }
        if(empty($end_time)){
            $this->G->error("end_time_error");
            return null;
        }
        $this->Messages->deleteAll(['add_time >'=>strtotime($begin_time), 'end_time >'=>strtotime($end_time),'is_read'=>1]);
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.07.03
   * ps：获取消息列表
   * */
    public function getList()
    {
        $is_read = $this->request->getData("is_read");
        $page = $this->request->getData("page");
        $page = empty($page) ? 1 : $page;
        $where = [];
        if(is_numeric($is_read)){
            $where['is_read'] = $is_read;
        }
        $messages = $this->Messages->find()
            ->where($where)
            ->order(['is_read desc','id desc'])
            ->limit($this->displayNum)
            ->page($page);
        $re = [];
        $count = $messages->count();
        $re['pageTotal'] = ceil($count/$this->displayNum);
        $messages = $messages->toArray();
        $re['list'] = [];
        if(!empty($messages)){
            foreach ($messages as $k=>$v){
                $messages[$k]['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
            }
        }
        $this->G->success('Success', $re);
    }

}
?>